var http = require('http')
var spawn = require('child_process').spawn

// 脚本地址
const script = './autoBuild.sh';
// 执行指令的地址
const currentPath = '/www/wwwroot/520.yxwjj.top/20200711/test';
const password = '123456';
http.createServer(function (req, res) {
    if(req.headers['x-gitee-token'] === password) {
        console.log('密码正确 开始拉取代码 --- suc');
        rumCommand('sh', [script], function( txt ) {
            console.log(`在${currentPath}目录下执行了脚本${script}`);
            console.log(txt);
        });
        res.statusCode = 200;
        res.end('success  webhook git');
    } else {
        console.log('cccc')
        res.statusCode = 200;
        res.end('??? 123');
    }
}).listen(3000);
console.log(`http://127.0.0.1:3000`)

function rumCommand( cmd, args, callback ) {
    var child = spawn( cmd, args, {
        cwd: currentPath,
    })
    var response = ''
    child.stdout.on('data', function( buffer ){ response += buffer.toString(); })
    child.stdout.on('end', function(){ callback( response ) })
}
